package com.wx.wxpush.job;

import com.wx.wxpush.service.PushService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;


@Lazy(false)
@Component
@EnableScheduling
public class SpringDynamicCornTaskNight implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCornTaskNight.class);

    private static final String DEFAULT_CRON = "* * * * 1 ?";
    private String cron = DEFAULT_CRON;
    @Autowired
    private PushService pushService;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(() -> {
            System.out.println("真正的任务执行逻辑。。。");
          //pushService.goodMorning();

        }, triggerContext -> {
            CronTrigger trigger = new CronTrigger(cron);
            return trigger.nextExecutionTime(triggerContext);
        }
        );
    }

    public void setCron(String cron) {
        System.out.println("原来的cron："+this.cron+"更新后的cron："+cron);
        this.cron = cron;
    }
    public String getCron() {
        return this.cron;
    }
}
